<template>
  <erp-form-popover
    :title="title"
    :field="field"
    :value="value"
    :props="props"
    :extra="extra"
    :options="computedOptions"
    :form="form"
    @value-change="handleValueChange"
    @update-id-field="handleUpdateField"
    @single-select="handleSingleSelect"
  />
</template>

<script>
import ErpFormPopover from './ErpFormPopover';
import {base as request} from '@/request';
/**
 * 采购订单选择
 * @author maolin
 */
export default {
  name: "ErpFormPurOrder",
  components: {
    ErpFormPopover
  },
  props: {
    /**
     * 标题
     * @public
     */
    title: {
      type: String,
      default: '',
    },
    /**
     * 字段
     * @public
     */
    field: {
      type: String,
      default: ''
    },
    /**
     * 值
     * @public
     */
    value: {
      type: [String, Number],
      default: ''
    },
    /**
     * 属性，暂未使用
     * @public
     */
    props: {
      type: Object,
      default () {
        return {
        };
      }
    },
    /**
     * 额外参数
     * ```js
     * return {
          // 表单需要的id字段
          idField: '',
          inputPlaceholder: '',
          modalTitle: '',
          modalWidth: null,
          modalHeight: null,
          // 模态框表格中name对应的字段名
          modalNameField: null,
          // 模态框表格中id对应的字段名
          modalIdField: null,
          // 是否转换为字符串
          needToString: false,
          // 关联额外字段，除了id和name之外还需要关联字段时
          materialType: null,
          extraData: [
            // {
            //     // 外部id
            //     field: "userId",
            //     // 内部id
            //     relatedField: "id",
            // }
          ],
        }
     * ```
     * @public
     */
    extra: {
      type: Object,
      default () {
        return {
          // 表单需要的id字段
          idField: '',
          inputPlaceholder: '',
          modalTitle: '',
          modalWidth: null,
          modalHeight: null,
          // 模态框表格中name对应的字段名
          modalNameField: null,
          // 模态框表格中id对应的字段名
          modalIdField: null,
          // 是否转换为字符串
          needToString: false,
          // 关联额外字段，除了id和name之外还需要关联字段时
          materialType: null,
          extraData: [
            // {
            //     // 外部id
            //     field: "userId",
            //     // 内部id
            //     relatedField: "id",
            // }
          ],
        };
      }
    },
    /**
     * 表单对象
     * @public
     */
    form: {
      type: Object,
      default () {
        return {};
      }
    }
  },
  data() {
    return {
    };
  },
  computed: {
    materialType () {
      return this.extra && this.extra.materialType ? this.extra.materialType : null;
    },
    computedOptions () {
      return {
        "type": "singleSelect",
        "title": "所属订单",
        "table": {
          "props": {
            "resizable": true
          },
          "permissions": ["purOrderBack:ordersBytype"],
          "rowKey": "id",
          "list": [],
          "fields": [
            {"label": "单号", "width": "150", "fixed": null, "prop": "no", "sortable": "custom"},
            {
              "label": "供应商/客户",
              "width": "120",
              "fixed": null,
              "prop": "bizUserId",
              "showTextProp": "bizUserIdShowText",
              "sortable": "custom"
            },
            {"label": "备注", "width": "", "fixed": null, "prop": "remark"},
            {
              "label": "订单日期",
              "width": "120",
              "fixed": null,
              "prop": "orderDate",
              "formatter": ["formatDate", "{y}-{m}-{d}"],
              "sortable": "custom"
            }
          ],
          "border": false,
          "showPagination": true,
          "showOrderNumber": false,
          "selectable": false,
          "defaultExpandAll": true,
          "showExport": false,
          "customColumn": false,
          "operationWidth": "100"
        },
        "search": {
          "form": {
            "pageOrderBy": null,
            "pageOrderDir": null,
            "pageIndex": 1,
            "pageSize": 30
          },
          "formFunction": "return {id: data.type, materialType: " + this.materialType + "};",
          "showKeywordQuery": true,
          "keywordKey": "no",
          "headSearchPlaceholder": "所属订单",
          "showAdvancedQuery": false,
        },
        "apiList": {
          "list": "/purOrderBack/ordersBytype"
        },
        "components": {},
        "buttons": []
      };
    }
  },
  watch: {
  },
  created() {
  },
  mounted() {
  },
  methods: {
    /**
     * 监听值变化
     * @private
     * @param value
     */
    handleValueChange (value) {
      this.$emit('value-change', value);
    },
    /**
     * 监听表单其他值更新
     * @private
     * @param {object} obj 其他值对象{表单字段1：表单值1，表单字段2： 表单值2}
     */
    handleUpdateField (obj) {
      this.$emit('update-id-field', obj);
    },
    /**
     * 单选数据
     * @param {object} data 当前选中行
     * @public
     */
    handleSingleSelect (data) {
      // 订单类型id
      const id = data.form.type;
      // 订单id
      const orderId = data.item.id;
      request.post('/DataObjController/ordersDetail', {
        id, orderId
      })
        .then(res => {
          const data = res.data;
          this.$emit('update-id-field', {
            currencyId: data.currencyId
          });
          if (data.list.length > 0) {
            this.$msgbox({
              title: '提示',
              message: '检测到当前订单存在明细，是否需要覆盖明细？覆盖会造成原先录入的明细丢失！',
              confirmButtonText: '覆盖',
              cancelButtonText: '取消',
              showCancelButton: true,
              type: 'warning'
            }).then(() => {
              const list = data.list.map(item => {
                let obj = {};
                obj.materialId = item.materialId;
                obj.materialIdShowText = item.materialIdShowText;
                obj.mainUnit = item.mainUnit;
                obj.backUnit = item.purUnit;
                obj.productType = item.materialShape;
                obj.priceUnitOriginal = item.priceUnitOriginal;
                obj.remark = item.remark;
                return obj;
              });

              this.$emit('update-id-field', {
                list: list
              });
            }).catch(() => {});
          }
        });
    }
  },
};
</script>

<style lang="scss" scoped>

</style>
